package com.qf.flash.ui.circle;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.qf.flash.base.BaseRefreshFragment;
import com.qf.flash.model.VideoListBean;
import com.qf.flash.mvp.contract.VideoContract;
import com.qf.flash.mvp.presenter.VideoListPresenter;
import com.qf.flash.ui.circle.adapter.VideoItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.widget.ViewUtil;

import java.util.List;

public class VideoFragment extends BaseRefreshFragment implements VideoContract.View {
//    @BindView(R.id.rv)
//    RecyclerView mRecyclerView;
//    Unbinder unbinder;
//    @BindView(R.id.mRefresh)
//    SmartRefreshLayout mRefresh;
    private RVAdapter<Object> mAdapter;
    private VideoListPresenter videoListPresenter;
    private int pageNo = 1, pageSize = 30;

//    @Override
//    protected int setContentView() {
//        return R.layout.fragment_recommend;
//    }

//    @Override
//    protected void initView() {
//        mRefresh.setOnRefreshListener(this);
//        mRefresh.setOnLoadMoreListener(this);
//    }

    @Override
    protected void requestData(boolean isRefresh, int page) {
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
        videoListPresenter.getVideos(pageNo,pageSize);
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new GridLayoutManager(getContext(), 2);
    }

    @Override
    public RecyclerView.Adapter getRecyclerAdapter() {
        mAdapter = new RVAdapter<>(new VideoItem());
        return mAdapter;
    }


    @Override
    protected void lazyLoad() {

//        mAdapter = new RVAdapter<>(new VideoItem());
//        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));

//        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new MyDividerItem(15, 10));
        mRefresh.autoRefresh();
//        mRefresh.setEnableLoadMore(false);
    }


    public static VideoFragment newInstance() {
        VideoFragment videoFragment = new VideoFragment();
        return videoFragment;
    }


    @Override
    public void setShowDatas(List<VideoListBean> videoListBeans,int totalPage) {
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
        List<Object> datas = mAdapter.getDatas();
        mRefresh.setEnableLoadMore(page < totalPage);

        if (isRefresh) {
            datas.clear();
        }
        datas.addAll(videoListBeans);
        mAdapter.notifyDataSetChanged();

       /*if(isFresh){
            datas.clear();
           datas.addAll(videoListBeans);
           mRefresh.finishRefresh();
       }else if(isLoadMore){
           datas.addAll(videoListBeans);
           mRefresh.finishLoadMore();
       }

        mAdapter.notifyDataSetChanged();
        isFresh = false;
        isLoadMore = false;*/
    }
    private boolean isFresh,isLoadMore;
   /* @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            isFresh = true;
            pageNo = 1;
        videoListPresenter.getVideos(pageNo, pageSize);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isLoadMore = true;
        pageNo++;
        videoListPresenter.getVideos(pageNo, pageSize);
    }*/


    // 自定义条目修饰类
    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int leftRight;
        private int topBottom;

        public SpacesItemDecoration(int leftRight, int topBottom) {
            this.leftRight = leftRight;
            this.topBottom = topBottom;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        }
    }

    @Override
    protected void initInject() {
        super.initInject();
        videoListPresenter = new VideoListPresenter();
        initPresenters(videoListPresenter);
    }
}
